# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: xguo

import coin.exchange.base.kr_rest.native_public_client_base as npubcb
import coin.exchange.huobi.kr_rest.constants as exchange_constants


class HuobiNativePublicClient(npubcb.NativePublicClientBase):
  # According to Huobi API documentation, you must add this header fields.
  user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 ' \
               '(KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36'
  headers = {
      "Content-type": "application/x-www-form-urlencoded",
      'User-Agent': user_agent,
  }

  def __init__(self):
    npubcb.NativePublicClientBase.__init__(self, url="https://api.huobi.pro")
    self._valid_kline_period = [
        '1min', '5min', '15min', '30min', '60min', '1day', '1mon', '1week', '1year'
    ]

  def check_kline_period(self, kline_period):
    assert kline_period in self._valid_kline_period, \
      'Invalid kline period!'

  def get_ticker(self, symbol):
    return self._get_as_json("market/detail/merged",
                             params={'symbol': symbol},
                             headers=self.headers)

  def get_orderbook(self, symbol, step_type='step1'):
    return self._get_as_json("market/depth",
                             params={
                                 'symbol': symbol, 'type': step_type
                             },
                             headers=self.headers)

  def get_history_kline(self, symbol, kline_period, size):
    self.check_kline_period(kline_period)
    params = {'symbol': symbol, 'period': kline_period, 'size': size}
    return self._get_as_json("market/history/kline", params=params, headers=self.headers)

  def queryAll(self):
    for symbol in exchange_constants.active_symbol_list:
      print(self.get_orderbook(symbol))


if __name__ == "__main__":
  client = HuobiNativePublicClient()
  print(client.get_ticker('xrpbtc').msg)
  print(client.get_orderbook('eosbtc').msg)
  print(client.get_histroy_kline('btcusdt', '1min', 5))
  client.queryAll()
